package service;

import domain.Users;
import domain.product;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserService {
    private JdbcTemplate jdbcTemplate;
    private String sql;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

//   批量添加
    public void batchUpdate(final List<Users> users) {
        sql = "insert into users (uname,upwd) values (?,?)";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                Users u = users.get(i);
                preparedStatement.setString(1,u.getUname());
                preparedStatement.setString(2,u.getUpwd());
            }

            @Override
            public int getBatchSize() {
                return users.size();
            }
        });
    }

//   模糊查询
    public List<Map<String,Object>> selectUsers(String s){
        sql = "select uid,uname,upwd from Users where uname like concat('%', ? ,'%')";
        return jdbcTemplate.queryForList(sql);
    }
}
